#include "main.h"
#include "motor.h"

void MOTOR_CONTROL(uint16_t direction)//对电机进行控制，使用八拍
{
	static uint8_t step=0;
	if(direction==MOTOR_DIR_CCW)//控制电机方向
	{
		if(step==0)
		{
			step=8;
		}
		
	}	
	else if(direction==MOTOR_DIR_CW)
	{
		if(step==8)
		{
			step=0;
		}
	}

	switch (step) {
        case 0:
            MOTOR_A_H; MOTOR_B_L; MOTOR_C_L; MOTOR_D_L;
            break;
        case 1:
            MOTOR_A_H; MOTOR_B_H; MOTOR_C_L; MOTOR_D_L;
            break;
        case 2:
            MOTOR_A_L; MOTOR_B_H; MOTOR_C_L; MOTOR_D_L;
            break;
        case 3:
            MOTOR_A_L; MOTOR_B_H; MOTOR_C_H; MOTOR_D_L;
            break;
        case 4:
            MOTOR_A_L; MOTOR_B_L; MOTOR_C_H; MOTOR_D_L;
            break;
        case 5:
            MOTOR_A_L; MOTOR_B_L; MOTOR_C_H; MOTOR_D_H;
            break;
        case 6:
            MOTOR_A_L; MOTOR_B_L; MOTOR_C_L; MOTOR_D_H;
            break;
        case 7:
            MOTOR_A_H; MOTOR_B_L; MOTOR_C_L; MOTOR_D_H;
            break;
        default:
            break;
    }

	if(direction)
	{
		step--;
	}
	else
	{
		step++;
	}
}

void Step_MOTOR_Control(uint16_t angle,uint8_t direction)//电机启动
{
	int i=0;
	int pulse =(int)((float)(angle*64/5.625));//脉冲与步距角的关系
	for(i=0;i<pulse;i++)
	{
		MOTOR_CONTROL(direction);
		HAL_Delay(2);
	}
}

void Step_MOTOR_Start()
{
	MOTOR_A_H;
	MOTOR_B_L;
	MOTOR_C_L;
	MOTOR_D_L;
}

void Step_MOTOR_Reset(void)//四相都是低电平代表电机停止
{
	MOTOR_A_L;
	MOTOR_B_L;
	MOTOR_C_L;
	MOTOR_D_L;
}
